package com.lishe.lishe.lishe.lisheDB;

import android.content.ContentValues;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.lishe.lishe.lishe.module.excitation.bean.user.UserObject;

/**
 * Created by shino on 2017/12/26.
 */

public class LiSheDB extends SQLiteOpenHelper {
    private static final int VERSION = 1;
    private static final String DB_NAME = "lishe_db";
    private static final String ID = "id";
    private static final String PHONE = "phone";//手机号码
    private static final String COM_SIZE = "com_size";//企业数
    private static final String COM_ID = "com_id";//企业id
    private static final String COM_NAME = "com_name";//企业名称
    private static final String USER_ID = "user_id";//企业id
    private static final String USER_NAME = "user_name";//用户名
    private static final String TABLE_NAME = "com_user_table";//一企一舍用户对象

    private static final String CREAT_TABLE = "CREATE TABLE " + TABLE_NAME
            + "(" + ID + " integer primary key autoincrement, "
            + USER_ID + " TEXT "
            + USER_NAME + " TEXT "
            + PHONE + " TEXT ,"
            + COM_ID + " TEXT , "
            + COM_NAME + " TEXT , "
            + COM_SIZE + " integer , "
            + ") ";

    public LiSheDB(Context context) {
        super(context, DB_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREAT_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    /**
     * 添加插入
     *
     * @param userObject
     * @param comsize
     */
    public void addUser(UserObject userObject, int comsize) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(USER_ID, userObject.getUserId());
        values.put(USER_NAME, userObject.getEmpName());
        values.put(PHONE, userObject.getPhoneNo());
        values.put(COM_ID, userObject.getComId());
        values.put(COM_NAME, userObject.getComName());
        values.put(COM_SIZE, comsize);
        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    /**
     * 删除
     * @param userid
     */
    public void delecteUser(String userid) {
        SQLiteDatabase db = getWritableDatabase();
        db.delete(TABLE_NAME, USER_ID + "=?", new String[]{userid});
        db.close();
    }

    public void upDataUser(String userid) {

    }

}
